{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import datetime\n",
    "\n",
    "# 1. 输入某年某月某日，判断这一天是这一年的第几天\n",
    "def day_of_year():\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "    date = datetime.date(year, month, day)\n",
    "    day_of_year = date.timetuple().tm_yday\n",
    "    print(f\"{year}年{month}月{day}日是{year}年的第{day_of_year}天\")\n",
    "\n",
    "# 2. 判断1000中有多少个素数，并输出所有素数\n",
    "def prime_numbers():\n",
    "    primes = []\n",
    "    for num in range(2, 1001):\n",
    "        is_prime = True\n",
    "        for i in range(2, int(math.sqrt(num)) + 1):\n",
    "            if num % i == 0:\n",
    "                is_prime = False\n",
    "                break\n",
    "        if is_prime:\n",
    "            primes.append(num)\n",
    "    print(f\"1000以内的素数有 {len(primes)} 个，分别是: {primes}\")\n",
    "\n",
    "# 3. 利用条件运算符的嵌套来完成此题：学习成绩 >= 90 分的用 A 表示，60-89 分之间的用 B 表示，60 分以下的用 C 表示。\n",
    "def grade_classifier():\n",
    "    score = int(input(\"请输入成绩: \"))\n",
    "    result = \"A\" if score >= 90 else \"B\" if score >= 60 else \"C\"\n",
    "    print(f\"成绩等级为: {result}\")\n",
    "\n",
    "# 4. 输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def string_statistics():\n",
    "    text = input(\"请输入一行字符串: \")\n",
    "    letters = sum(c.isalpha() for c in text)\n",
    "    spaces = sum(c.isspace() for c in text)\n",
    "    digits = sum(c.isdigit() for c in text)\n",
    "    others = len(text) - letters - spaces - digits\n",
    "    print(f\"字母个数: {letters}, 空格个数: {spaces}, 数字个数: {digits}, 其它字符个数: {others}\")\n",
    "\n",
    "# 5. 找出1000以内的所有完数\n",
    "def perfect_numbers():\n",
    "    perfects = []\n",
    "    for num in range(1, 1001):\n",
    "        divisors_sum = sum(i for i in range(1, num) if num % i == 0)\n",
    "        if divisors_sum == num:\n",
    "            perfects.append(num)\n",
    "    print(f\"1000以内的完数有: {perfects}\")\n",
    "\n",
    "# 6. 输入任意数 N，求 1 + 2! + 3! + ... + N! 的值\n",
    "def factorial_sum():\n",
    "    N = int(input(\"请输入任意正整数 N: \"))\n",
    "    result = sum(math.factorial(i) for i in range(1, N + 1))\n",
    "    print(f\"1 + 2! + 3! + ... + {N}! 的值为: {result}\")\n",
    "\n",
    "# 7. 输入任意奇数 N，按如下要求打印出图案（菱形）\n",
    "def print_diamond():\n",
    "    N = int(input(\"请输入一个奇数 N: \"))\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入奇数\")\n",
    "        return\n",
    "    # 打印上半部分\n",
    "    for i in range(1, N + 1, 2):\n",
    "        print(\" \" * ((N - i) // 2) + \"*\" * i)\n",
    "    # 打印下半部分\n",
    "    for i in range(N - 2, 0, -2):\n",
    "        print(\" \" * ((N - i) // 2) + \"*\" * i)\n",
    "\n",
    "# 8. 对 10 个数进行排序\n",
    "def sort_numbers():\n",
    "    numbers = list(map(int, input(\"请输入 10 个数，用空格分开: \").split()))\n",
    "    numbers.sort()\n",
    "    print(f\"排序后的数字为: {numbers}\")\n",
    "\n",
    "# 9. 从键盘输入一个数，按原来的规律将它插入已排序数组中\n",
    "def insert_into_sorted_array():\n",
    "    sorted_array = list(map(int, input(\"请输入一个已排好序的数组，用空格分开: \").split()))\n",
    "    num_to_insert = int(input(\"请输入要插入的数字: \"))\n",
    "    sorted_array.append(num_to_insert)\n",
    "    sorted_array.sort()\n",
    "    print(f\"插入后的数组为: {sorted_array}\")\n",
    "\n",
    "# 10. 有 n 个人围成一圈，从第一个人开始报数（从 1 到 3 报数），凡报到 3 的人退出，问最后留下的是原来第几号的那位\n",
    "def josephus_problem():\n",
    "    n = int(input(\"请输入人数 n: \"))\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)  # 报数到 3 的人退出\n",
    "        people.pop(index)\n",
    "    print(f\"最后留下的人是原来第 {people[0]} 号的那位\")\n",
    "\n",
    "# 主菜单，用户选择运行不同的功能\n",
    "def main():\n",
    "    while True:\n",
    "        print(\"请选择要执行的程序：\")\n",
    "        print(\"1. 输入某年某月某日，判断这一天是这一年的第几天\")\n",
    "        print(\"2. 判断1000中有多少个素数，并输出所有素数\")\n",
    "        print(\"3. 学习成绩分类\")\n",
    "        print(\"4. 字符串统计\")\n",
    "        print(\"5. 找出1000以内的所有完数\")\n",
    "        print(\"6. 求 1 + 2! + 3! + ... + N! 的值\")\n",
    "        print(\"7. 打印菱形\")\n",
    "        print(\"8. 对 10 个数进行排序\")\n",
    "        print(\"9. 插入数字到已排序数组\")\n",
    "        print(\"10. 约瑟夫问题\")\n",
    "        print(\"0. 退出\")\n",
    "\n",
    "        choice = input(\"请输入数字选择程序: \")\n",
    "\n",
    "        if choice == \"1\":\n",
    "            day_of_year()\n",
    "        elif choice == \"2\":\n",
    "            prime_numbers()\n",
    "        elif choice == \"3\":\n",
    "            grade_classifier()\n",
    "        elif choice == \"4\":\n",
    "            string_statistics()\n",
    "        elif choice == \"5\":\n",
    "            perfect_numbers()\n",
    "        elif choice == \"6\":\n",
    "            factorial_sum()\n",
    "        elif choice == \"7\":\n",
    "            print_diamond()\n",
    "        elif choice == \"8\":\n",
    "            sort_numbers()\n",
    "        elif choice == \"9\":\n",
    "            insert_into_sorted_array()\n",
    "        elif choice == \"10\":\n",
    "            josephus_problem()\n",
    "        elif choice == \"0\":\n",
    "            print(\"退出程序\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效输入，请重新选择\")\n",
    "\n",
    "# 运行主程序\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
